import os

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

import datastore
import library


class CleanTeam(webapp.RequestHandler):
    def get(self):

     fac =  self.request.get('facility') 
     facilityid = library.getfacility(fac)
     print("facilityid = " + facilityid )

     query = 'select __key__ from FacilityTeam where facility=' + facilityid 
     print query
     keys =  db.GqlQuery( query )
     for k in keys:
       t = db.get(k)
       print "delete ",t.name,t.captain  # ,t.key()
       t.delete()



class CleanPlayer(webapp.RequestHandler):
    def get(self):

       fac =  self.request.get('facility') 
       facilityid = library.getfacility(fac)
       print("facilityid = " + facilityid )

       query = "select __key__ from FacilityPlayer where facility="+facilityid
       print query
       keys =  db.GqlQuery( query )
       for k in keys:
          p = db.get(k)
          print "delete ",p.id,p.fname,p.lname
          p.delete()


class CleanRoster(webapp.RequestHandler):
    def get(self):

       fac =  self.request.get('facility') 
       facilityid = library.getfacility(fac)
       print("facilityid = " + facilityid )

       query = "select __key__ from FacilityRoster"
       query = "select __key__ from FacilityRoster where facility="+facilityid
       print query

       keys =  db.GqlQuery( query )
       for k in keys:
          r = db.get(k)
          print "delete ",r.fname,r.lname #r.key()
          r.delete()


class MainPage(webapp.RequestHandler):
    def get(self):
        print("specify cleanteam?facility=sctc "  )
        print("specify cleanroster?facility=lp "  )
        print("specify cleanplayer?facility=cup "  )

application = webapp.WSGIApplication(
                                     [('/', MainPage),
                                     ('/cleanteam', CleanTeam),
                                     ('/cleanroster', CleanRoster),
                                     ('/cleanplayer', CleanPlayer)],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":

#   print __file__
#   print __name__

    main()
